<template>
  <div>
    <el-dialog
      title="修改密码"
      :visible.sync="visible"
      width="500px"
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="新密码" prop="password">
          <el-input v-model="form.password" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="loading" @click="post">更 改</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { completeDept } from '@/api/dept'
import { changePwd } from '@/api/password'
export default {
  props: {
    id: {
      default: 0
    },
    dialogVisible: false
  },
  data() {
    return {
      form: {
        password: ''
      },
      rules: {
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 18,
            message: '密码长度在6-18位',
            trigger: 'blur'
          }
        ]
      },
      loading: false
    }
  },
  computed: {
    visible: {
      get() {
        return this.dialogVisible
      },
      set(visible) {
        this.$emit('update:dialogVisible', visible)
      }
    }
  },
  watch: {
    id(old, val) {
      completeDept({ id: this.id }).then(res => {
        this.dept = res.data
      })
    }
  },
  created() {
    // this.getDepts();
  },
  mounted() {},
  beforeDestroy() {},
  methods: {
    post() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          changePwd({
            user_id: this.id,
            password: this.form.password
          }).then(res => {
            this.$notify({
              title: '成功',
              message: '密码修改成功',
              type: 'success'
            })
            this.$emit('finish')
            this.loading = false
          })
        } else {
        }
      })
    }
  }
}
</script>
